/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax;

import deatax.network.Client;
import deatax.network.Messages;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 * @author nrechten
 */
public class TrayMenu {



    private TrayIcon trayIcon;

    private MenuItem open;
    private MenuItem endTurn;
    private MenuItem closeTray;
    private MenuItem player;

    private PopupMenu trayMenu;
    private GameData data;

    public TrayMenu(GameData data) {

        this.data = data;
        if (SystemTray.isSupported()) {

            SystemTray tray = SystemTray.getSystemTray();
            Image image = Toolkit.getDefaultToolkit().getImage("display/tray.png");

            System.out.println(image);
            trayMenu = new PopupMenu();
            player = new MenuItem("player");
            player.setEnabled(false);
            closeTray = new MenuItem("Close Tray icon");
            closeTray.addActionListener(new CloseListener());
            open = new MenuItem("Open Main window");
            open.addActionListener(new OpenListener());
            endTurn = new MenuItem("End Turn");
            endTurn.addActionListener(new EndTurnListener());
            trayMenu.add(player);
            trayMenu.addSeparator();
            trayMenu.add(open);
            trayMenu.add(endTurn);
            trayMenu.addSeparator();
            trayMenu.add(closeTray);

            
            trayIcon = new TrayIcon(image, "Taxography", trayMenu);
            trayIcon.setImageAutoSize(true);
            try {
                tray.add(trayIcon);
            } catch (AWTException ex) {
                Logger.getLogger(TrayMenu.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void notifyTray(String message, boolean yourTurn) {
        if(!data.getFrame().isVisible() || !data.getFrame().isActive()){
            if(message != null && trayIcon != null){
                trayIcon.displayMessage("Taxography",message, TrayIcon.MessageType.INFO);
            }
        }
        endTurn.setEnabled(yourTurn);
    }

    private class EndTurnListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            Client.get().sendMessage(Messages.END_TURN);
        }
    }

    private class CloseListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            SystemTray tray = SystemTray.getSystemTray();
            tray.remove(trayIcon);
        }
    }

    private class OpenListener implements ActionListener {

        public OpenListener() {
        }

        public void actionPerformed(ActionEvent e) {
            data.getFrame().setVisible(true);
            data.getFrame().requestFocus();
            data.getFrame().setExtendedState(JFrame.NORMAL);
        }
    }

    public void setPlayer(Player player){
//        this.player.
        this.player.setLabel(player.getName() + "'s turn");
    }
}
